package com.ruoyi.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.demo.domain.vo.meetDetailVO;
import com.ruoyi.demo.domain.vo.meetLogVO;
import com.ruoyi.demo.mapper.MeetDetailMapper;
import com.ruoyi.demo.service.IMeetDetailService;
import com.ruoyi.system.domain.Agenda;
import com.ruoyi.system.domain.Meet;
import com.ruoyi.system.domain.MeetMaterials;
import com.ruoyi.system.domain.Room;
import com.ruoyi.system.domain.vo.MeetUserAttented;
import com.ruoyi.system.mapper.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;
@Service
public class MeetDetailServiceImpl extends ServiceImpl<MeetDetailMapper, meetDetailVO> implements IMeetDetailService {
    @Resource
    MeetDetailMapper meetDetailMapper;

    @Resource
    MeetMapper meetMapper;

    @Resource
    RoomMapper roomMapper;

    @Resource
    MeetMaterialsMapper meetMaterialsMapper;

    @Resource
    AgendaMapper agendaMapper;

    @Resource
    SysUserMapper userMapper;

    @Override
    public meetDetailVO getMeetDetailById(int meetId) {
        meetDetailVO meetDetail = meetDetailMapper.getMeetDetailById(meetId);
        List<String> userDetailById = meetDetailMapper.getMeetUserDetailById(meetId);
        meetDetail.setAttendUser(String.join(",", userDetailById));
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String formattedStartTime = dateFormat.format(meetDetail.getStartTime());
            meetDetail.setStartTime1(formattedStartTime);
            String formattedEndTime = dateFormat.format(meetDetail.getEndTime());
            meetDetail.setEndTime1(formattedEndTime);
            String formattedCreatedAt = dateFormat.format(meetDetail.getCreatedAt());
            meetDetail.setCreatedAt1(formattedCreatedAt);
            meetDetail.setCreatedAt(null);
            meetDetail.setEndTime(null);
            meetDetail.setStartTime(null);
        return meetDetail;
    }

    @Override
    public Boolean getIsMeetReserve(int roomId, String reserveEndTime, String reserveStartTime) {

        if(meetDetailMapper.getIsMeetRequestsReserve(roomId,reserveEndTime,reserveStartTime)==1){
            return meetDetailMapper.getIsMeetReserve(roomId,reserveEndTime,reserveStartTime)==1;
        }
        return false;
    }

    @Override
    public meetLogVO selectMeetInFo(Long meetId) {

        meetLogVO vo = new meetLogVO();

        // 设置会议信息
        Meet meet = meetMapper.selectMeetByMeetId(Math.toIntExact(meetId));
        vo.setMeet(meet);

        // 设置会议室信息
        Room room = roomMapper.selectRoomByRoomId(Long.valueOf(meet.getRoomId()));
        vo.setRoom(room);

        // 设置会议材料
//        MeetMaterials materials = meetMaterialsMapper.selectMeetMaterialsById(meetId);
//        vo.setMeetMaterials(materials);

        // 设置议程列表
        List<Agenda> agendaList = agendaMapper.selectAgendaByMeetId(meetId);
        vo.setAgendaList(agendaList);

        // 设置参会人员列表
        List<MeetUserAttented> userList = userMapper.selectAttentedUsersByMeetId(meetId);
        vo.setUserList(userList);

        return vo;
    }

}
